import { queryInfo } from '@/services/user';
import { queryLocation, querySubordinate } from '@/services/location';

const ZROE = {
  '-2': 1,
  0: 1,
  2: 100,
  4: 10000,
  6: 1000000,
  8: 1000000
}

function getLen(value) {
  return (+(value % 1e2 === 0) + +(value % 1e4 === 0)) * 2;
}

export default {
  namespace: 'location',

  state: {},

  effects: {
    *queryList(_, { call, put, select }) {
      const response = yield call(queryLocation);
      let user = yield select(state => state.user.details);
      if (!user) {
        user = yield call(queryInfo);
        yield put({ type: 'user/setInfo', payload: user })
      }
      
      const ans = response.map(it => ({
        label: it.shortName,
        value: it.id,
        isLeaf: it.level > 3
      }))
      yield put({ type: 'setProvice', payload: ans })
    },
    *querySubordinate({ payload }, { call, put, select }) {
      const parentLen = 6 - getLen(+payload);  //获取父级有效长度330200 有效长度为4
      let user = yield select(state => state.user.details);
      if (!user) {
        user = yield call(queryInfo);
        yield put({ type: 'user/setInfo', payload: user })
      }

      const response = yield call(querySubordinate, payload);

      let ans = response.map(it => ({
        label: it.shortName,
        value: it.id,
        isLeaf: it.level > 3
      }))
      // .filter(v => ansExtend.includes(v.value) || ansExtend.length === 0 || ansExtend.some(r => +r === +payload ))
      yield put({ type: 'setSub', payload: ans });

    },
  },

  reducers: {
    setProvice(state, action) {      
      return {
        ...state,
        provice: action.payload
      }
    },
    setSub(state, action) {
      return {
        ...state,
        sub: action.payload
      }
    },
    
  },
};

